package tk.mynoteapp.dev.demo21.templatemethod.wrong;

import tk.mynoteapp.dev.demo21.templatemethod.Db;
import tk.mynoteapp.dev.demo21.templatemethod.right.AbstractCart;

import java.util.HashMap;
import java.util.Map;

public class TestDemo {
    public static void main(String[] args) {
        long userId = 2;
        Map<Long, Integer> items = new HashMap<>();
        items.put(1L, 5);

    }

    public void wrong(long userId, Map<Long, Integer> items) {
        String userCategory = Db.getUserCategory(userId);
        if (userCategory.equals("Normal")) {
            NormalUserCart normalUserCart = new NormalUserCart();
            System.out.println(normalUserCart.process(userId, items));
        }
        if (userCategory.equals("Vip")) {
            VipUserCart vipUserCart = new VipUserCart();
            System.out.println(vipUserCart.process(userId, items));
            System.out.println();
        }
        if (userCategory.equals("Internal")) {
            InternalUserCart internalUserCart = new InternalUserCart();
            System.out.println(internalUserCart.process(userId, items));
            System.out.println();
        }
    }

    public void right(long userId, Map<Long, Integer> items) {
        String userCategory = Db.getUserCategory(userId);
        // 可以利用Spring IoC容器根据名字渠道对应的Bean进行处理，从而消除if判断（工厂模式+模板方法模式）
        AbstractCart cart = null;// (AbstractCart) applicationContext.getBean(userCategory + "UserCart");
        System.out.println(cart.process(userId, items));
    }
}
